﻿<template>
  <div v-if="config.other.dataSave && config.other.dataSave.MetaSettings">
    <el-switch
      style="display: block"
      v-model="inc_fc"
      active-color="#13ce66"
      inactive-color="#ff4949"
      active-text="含默认栏位"
      inactive-text="不含默认栏位"
      @change="changeInc">
    </el-switch>
    <br>
    <el-select v-model="config.other.dataSave.MetaSettings.table" filterable  placeholder="请选择" @change="changeMeta">
      <el-option
        v-for="(item,index) in metatables"
        :key="index"
        :label="item.metadesc"
        :value="item.metauid">
        <span style="float: left">{{ item.metadesc }}</span>
        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.metaname }}</span>
      </el-option>
    </el-select>
    <el-divider></el-divider>
    <el-form-item label = "固定更新值">
      <label class="tips">提示:值设定请参考知识库中的<a target="_blank" href="http://fjkb.whfjwl.com/mdShow.html?defaultPk=%7B%22defaultPk%22%3A%224c83f52d-e6c4-4d85-a143-507a852399ca%22%2C%22version%22%3A%221.0.0%22%7D">【ICP前端框架新版设计】</a>中的“事件词典表”</label>
      <div>
        <el-button style="float: right; padding: 3px 0" type="text" @click="addFixedData">添加</el-button>
        <div>
          <el-table :data = 'config.other.dataSave.MetaSettings.fixedData' style = "width:100%" empty-text = "暂无数据，请添加" border stripe>
            <el-table-column align = "center" prop = 'text' label = '栏位'>
              <template slot-scope = "scope">
                <el-select v-model="scope.row.column" clearable placeholder="请选择">
                  <el-option
                    v-for="(item,index) in metaColumns"
                    :key="index"
                    :label="item.column_desc"
                    :value="item.column_name">
                    <span style="float: left">{{ item.column_desc }}</span>
                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.column_name }}</span>
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column align = "center" prop = 'settings' label = '值设定'>
              <template slot-scope="scope">
                <el-input v-model="scope.row.value" placeholder="请输入内容"></el-input>
              </template>
            </el-table-column>
            <el-table-column align = "center" label = "操作" width = "50">
              <template slot-scope = "scope">
                <el-button type = "text" size = "mini" @click = "handleFixedDataRemove(scope.$index)" title = '移除'>
                  <i class = "iconfont icon-shanchu"></i>
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-form-item>
    <slot>
    </slot>
  </div>
</template>
<script>
import { deepCopy } from '../commonUse'
export default {
  props: ['config'],
  data () {
    return {
      inc_fc: false,
      metatables: [],
      metaColumns: []
    }
  },
  mounted () {
    if (!this.config.other.dataSave) {
      this.$set(this.config.other, 'dataSource', {
        MetaSettings: {
          table: '',
          fixedData: []
        }
      })
    }
    if (!this.config.other.dataSave.MetaSettings) {
      this.$set(this.config.other.dataSave, 'MetaSettings', {
        table: '',
        fixedData: []
      })
    }
    if (!this.metatables || this.metatables.length <= 0) {
      this.changeInc()
    }
  },
  methods: {
    changeInc () {
      if (this.inc_fc) {
        this.metatables = this.$store.state.commonData.metaList_inc_fc
      } else {
        this.metatables = this.$store.state.commonData.metaList
      }
      this.$nextTick(() => {
        this.metaColumns = []
        let self = this
        var item = this.metatables.find(v => {
          return v.metauid === self.config.other.dataSave.MetaSettings.table
        })
        if (item) {
          this.metaColumns = item.columns
        }
      })
    },
    changeMeta () {
      this.metaColumns = []
      this.config.other.dataSave.MetaSettings.valueKey = ''
      this.config.other.dataSave.MetaSettings.labelKey = ''
      let self = this
      var item = this.metatables.find(v => {
        return v.metauid === self.config.other.dataSave.MetaSettings.table
      })
      if (item) {
        this.metaColumns = item.columns
        this.$emit('metaChanged', deepCopy(item), deepCopy(this.metatables))
      }
    },
    handleFilterRemove (index) {
      this.config.other.dataSave.MetaSettings.filterList.splice(index, 1)
    },
    handleOrderRemove (index) {
      this.config.other.dataSave.MetaSettings.orderList.splice(index, 1)
    },
    addFixedData () {
      this.config.other.dataSave.MetaSettings.fixedData.push({
        column: '',
        condition: '',
        value: ''
      })
    },
    handleFixedDataRemove (index) {
      this.config.other.dataSave.MetaSettings.fixedData.splice(index, 1)
    }
  },
  watch: {
  }
}
</script>
<style lang="scss">
</style>
